// todo: store
export default {
  // 开启命名空间
  namespaced: true,
  state: () => ({
    tlist: JSON.parse(localStorage.getItem('tlist') || '[]'),
    classFlag: 'all',
    isSel: ['all', 'no', 'yes'],
    istate: ['全部', '未完成', '已完成']
  }),
  actions: {},
  mutations: {
    // 追加数据
    listPush(state, value) {
      state.tlist.push(value)
    },
    // 删除数据
    listRemove(state, value) {
      const index = state.tlist.findIndex(obj => obj.id == value)
      state.tlist.splice(index, 1)
    },
    // 全选
    listAlls(state, value) {
      state.tlist.forEach(item => {
        item.isDone = value
      })
    },
    // 接受一个索引修改对应的 状态
    listBorder(state, value) {
      state.classFlag = state.isSel[value]
    },
    // 清除已完成的状态
    listClear(state) {
      state.tlist = state.tlist.filter(item => !item.isDone)
    }
  },
  getters: {
    // 反选
    isAll(state) {
      return state.tlist.every(item => item.isDone)
    },
    // 渲染不同的状态
    listState(state) {
      if (state.classFlag === 'yes') {
        return state.tlist.filter(item => item.isDone)
      } else if (state.classFlag === 'no') {
        return state.tlist.filter(item => !item.isDone)
      } else {
        return state.tlist
      }
    }
  }
}
